package com.xg.java.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xg.java.entity.SysUser;
import com.xg.java.resp.Response;
import com.xg.java.service.SysUserService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @description sys_user
 * @author wang
 * @date 2021-11-23
 */
@Slf4j
@Controller
@Api(tags = "用户管理")
@RequestMapping("/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;


    @Resource
    private SysUserService tUserService;


    @Resource
    private Response response;

    @GetMapping(value = "/test")
    public String test(String typeld,Model model) {
        model.addAttribute("typeld",typeld);
        return "xungu";
    }

    /**
     * 分页查询用户
     * @param current
     * @param request
     * @return
     */
    @ResponseBody
    @ApiOperation(value = "GET",notes = "分页查询用户")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = SysUser.class),
    })
    @RequestMapping(value = "/listTUser", produces = MediaType.APPLICATION_JSON_VALUE,method = RequestMethod.GET)
    public Response listUserOrderPage(@ApiParam(value = "通用的",name="current",required=true)int current, @ApiParam(value = "用户名",name="userName",required=false)String userName, HttpServletRequest request) {
        log.info("查询分页查询用户");
        IPage<SysUser> page = new Page<>(current, 10);
        page = this.tUserService.page(page, new LambdaQueryWrapper<SysUser>().eq(SysUser::getUserName,userName).orderByDesc(SysUser::getCreateTime));
        return response.success(0, "查询分页查询用户成功！", page);
    }

    

    /**
     * 添加用户
     * @return
     */
    @ApiOperation(value = "POST",notes = "添加用户")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK"),
    })
    @RequestMapping(value = "/addTUser", produces = MediaType.APPLICATION_JSON_VALUE,method = RequestMethod.POST)
    public Response addTUser(@RequestBody SysUser sysUser) {
        log.info("添加用户");
        sysUser.setCreateTime(new Date());
        boolean r = this.sysUserService.save(sysUser);
        if (r){
            return this.response.success(200,"添加用户成功");
        }else {
            return  this.response.failure(1873,"添加用户失败");
        }
    }

    /**
     * 修改用户
     * @return
     */
    @ApiOperation(value = "POST",notes = "修改用户")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK"),
    })
    @RequestMapping(value = "/editTUser", produces = MediaType.APPLICATION_JSON_VALUE,method = RequestMethod.POST)
    public Response editTUser(@RequestBody SysUser sysUser) {
        log.info("添加用户");
        sysUser.setPassword(null);
        boolean r = this.sysUserService.updateById(sysUser);
        if (r){
            return this.response.success(200,"修改用户成功");
        }else {
            return  this.response.failure(1873,"修改用户失败");
        }
    }

    /**
     * 删除用户
     * @return
     */
    @ApiOperation(value = "POST",notes = "删除用户")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK"),
    })
    @RequestMapping(value = "/delTUser", produces = MediaType.APPLICATION_JSON_VALUE,method = RequestMethod.POST)
    public Response delTUser(@RequestParam int id) {
        log.info("删除用户");
        boolean r = this.sysUserService.removeById(id);
        if (r){
            return this.response.success(200,"删除用户成功");
        }else {
            return  this.response.failure(1873,"删除用户失败");
        }
    }



}



